package com.awspaas.user.apps.cpq.dw;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExcelTransformListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOQueryAPI;

public class ComponentPartFormExcel  extends ExcelTransformListener{
	 public String getDescription() {
	        return "物料表单中，创建装配件时，上传装配件关联的装配体和采购件子项，如果上传的装配体和采购件在物流单价维护台账中不存在，这需要在台账中创建对应的数据，如果存在则需要把子项的数据补充完整。";
	    }
	@Override
	public Workbook fixExcel(ProcessExecutionContext pec, Workbook wb) {
		try {
			 // 时间点的常量见上表
	        String timeState = pec.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_EXCEL_TIMESTATE);// 通过该值判断当前事件所处的时间点
	        // 判断方式
	        	
	        //if (ListenerConst.FORM_EXCEL_TIMESTATE_IMPORT_BEFORE.equals(timeState)) {
	        if (ListenerConst.FORM_EXCEL_TIMESTATE_IMPORT_AFTER.equals(timeState)) {
	        	List<BO> bos=SDK.getBOAPI().query("BO_EU_CPQ_COMPONENT_PART_GRID").bindId(pec.getProcessInstance().getId()).list();
	        	if(bos.size()>0) {
	        		String sql="select * from BO_EU_CPQ_COMPONENT_PART where  BM!='' and BM is not null";
	        		List<RowMap> maps=DBSql.getMaps(sql);
	        		Map<String, RowMap> dataMap=new HashMap<String, RowMap>();
	        		for (RowMap rowMap : maps) {
						String bm=rowMap.getString("BM");												
						dataMap.put(bm, rowMap);
					}
	        		for (BO bo : bos) {
						String DJ=bo.getString("DJ");
						BigDecimal dj = new BigDecimal(DJ);
						if(dj.compareTo(BigDecimal.ZERO) == 0) {
							String bm=bo.getString("BM");																				
							if(dataMap.containsKey(bm)) {
							RowMap data=dataMap.get(bm);
							String name=data.getString("NAME");
							String DJ1=data.getString("DJ");
							BigDecimal dj1 = new BigDecimal(DJ1);
							if(dj1.compareTo(BigDecimal.ZERO) != 0) {
								String sl=bo.getString("SL");
								String price=data.getString("DJ");
								BigDecimal num1= new BigDecimal(sl);
						        BigDecimal num2 = new BigDecimal(price);
								bo.set("MC", name);
								bo.set("LX", data.getString("TYPE"));
								bo.set("GG", data.getString("GG"));
								bo.set("DH", data.getString("TH"));
								bo.set("DW", data.getString("DW"));
								bo.set("ZL", data.getString("ZL"));
								bo.set("TJ", data.getString("TJ"));
								bo.set("CLF", data.getString("CLF"));
								bo.set("RGF", data.getString("RGF"));
								bo.set("WXWG", data.getString("WXWG"));
								bo.set("CGF", data.getString("CGF"));
								bo.set("DJ", data.getString("DJ"));
								bo.set("ZJ", num1.multiply(num2));							
								String zl=data.getString("ZL");
								if(null!=zl&&!zl.isEmpty()) {
									 BigDecimal num3 = new BigDecimal(zl);
									bo.set("ZZL", num1.multiply(num3));
								}else {
									bo.set("ZZL", "0");
								}
								String tj=data.getString("TJ");
								if(null!=tj&&!tj.isEmpty()) {
									 BigDecimal num3 = new BigDecimal(tj);
									bo.set("ZTJ", num1.multiply(num3));
								}else {
									bo.set("ZTJ", "0");
								}
								String clf=data.getString("CLF");
								if(null!=clf&&!clf.isEmpty()) {
									 BigDecimal num3 = new BigDecimal(clf);
									bo.set("ZCLF", num1.multiply(num3));
								}else {
									bo.set("ZCLF", "0");
								}
								String rgf=data.getString("RGF");
								if(null!=rgf&&!rgf.isEmpty()) {
									 BigDecimal num3 = new BigDecimal(rgf);
									bo.set("ZGSF", num1.multiply(num3));
								}else {
									bo.set("ZGSF", "0");
								}
								String wx=data.getString("WXWG");
								if(null!=wx&&!wx.isEmpty()) {
									 BigDecimal num3 = new BigDecimal(wx);
									bo.set("ZWX", num1.multiply(num3));
								}else {
									bo.set("ZWX", "0");
								}
								String cgf=data.getString("CGF");
								if(null!=cgf&&!cgf.isEmpty()) {
									 BigDecimal num3 = new BigDecimal(cgf);
									bo.set("ZCGF", num1.multiply(num3));
								}else {
									bo.set("ZCGF", "0");
								}
								SDK.getBOAPI().update("BO_EU_CPQ_COMPONENT_PART_GRID", bo);
							}
							}else {
								//不存在该物料信息，则在物料单价台账中进行创建该信息
								String pid=SDK.getProcessAPI().createBOProcessInstance("obj_191624b652f041c1a21bba93fd88b319", pec.getUserContext().getUID(), "").getId();
							    BO newBo=new BO();
							    newBo.set("BM", bm);
							    newBo.set("NAME", bo.getString("MC"));
							    newBo.set("TYPE", bo.getString("LX"));
							    newBo.set("GG", bo.getString("GG"));
							    newBo.set("TH", bo.getString("DH"));
							    newBo.set("DW", bo.getString("DW"));
							    newBo.set("TS",  "可编辑");
							    newBo.set("STATUS", "拟制");
							    SDK.getBOAPI().create("BO_EU_CPQ_COMPONENT_PART", newBo, pid, pec.getUserContext().getUID());
							}
						}
					}
	        	}
//	        	  Sheet sheet =wb.getSheetAt(0);  // 获取第一个工作表
//	        	  List<String> lists=new ArrayList<String>();
//	        	    for (Row row : sheet) {
//	        	        for (Cell cell : row) {
//	        	            String cellValue = cell.toString();
//	        	            lists.add(cellValue);
//	        	            System.out.print(cellValue + "\t");
//	        	            
//	        	        }
//	        	        System.out.println();
//	        	    }
//	        	    
//	           System.out.println(lists.toString());
	        }
			
			return wb;
		} catch (Exception e) {
			throw e;
		}
		
	}

}
